/*
 *  Copyright (C) 2010 Fabien Renaud
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.dao.impl;

import com.boc.botv.common.Database;
import com.boc.botv.dao.BetDao;
import com.boc.botv.model.Bet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Fabien Renaud
 */
@Repository("betDao")
public class BetDaoImpl implements BetDao {

  private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  public List<Bet> getBets() {
    return (List<Bet>) Database.getSession().createQuery("from Bet").list();
  }

  public Bet getBet(Integer betId) {
    return (Bet) Database.getSession().load(Bet.class, betId);
  }

  public void deleteBet(Bet bet) {
    Database.getSession().delete(bet);
  }

  public void saveBet(Bet bet) {
    Database.getSession().save(bet);
  }

  public void updateBet(Bet bet) {
    Database.getSession().update(bet);
  }

  public List<Bet> getBetsBySubsection(int subsection) {
    return (List<Bet>) Database.getSession().createQuery("from Bet as b where b.subsection=" + subsection).list();
  }

  public List<Bet> getBetsByCreationTime(Date firstDate, Date lastDate) {
    String t1 = dateFormatter.format(firstDate);
    String t2 = dateFormatter.format(lastDate);
    return (List<Bet>) Database.getSession().createQuery("from Bet as bet where bet.creationTime between '" + t1 + "' and '" + t2 + "'").list();
  }

  public List<Bet> getBetsByStartTime(Date firstDate, Date lastDate) {
    String t1 = dateFormatter.format(firstDate);
    String t2 = dateFormatter.format(lastDate);
    return (List<Bet>) Database.getSession().createQuery("from Bet as bet where bet.startTime between '" + t1 + "' and '" + t2 + "'").list();
  }

  public List<Bet> getBetsByEndTime(Date firstDate, Date lastDate) {
    String t1 = dateFormatter.format(firstDate);
    String t2 = dateFormatter.format(lastDate);
    return (List<Bet>) Database.getSession().createQuery("from Bet as bet where bet.endTime between '" + t1 + "' and '" + t2 + "'").list();
  }

  public List<Bet> getBetsPlayedBetween(Date firstDate, Date lastDate) {
    String t1 = dateFormatter.format(firstDate);
    String t2 = dateFormatter.format(lastDate);
    // TODO
    return null;
  }

  public List<Bet> getBetsNotPlayedBetween(Date firstDate, Date lastDate) {
    String t1 = dateFormatter.format(firstDate);
    String t2 = dateFormatter.format(lastDate);
    // TODO
    return null;
  }

  public List<Bet> getBetsByCloserEndDate(int numberOfBet, int subsection) {
    return Database.getSession().createQuery("from Bet as b where b.subsection=" + subsection + " and b.endTime > NOW() order by b.endTime asc ").list();
  }

  public List<Bet> getCurrentBetsBySubsection(int subsection) {
    return Database.getSession().createQuery("from Bet as b where b.subsection=" + subsection + " and b.endTime > NOW()").list();
  }
}
